
export class Random {
    private _seed: number;

    constructor(seed: number) {
        this._seed = seed % 2147483647;
        if (this._seed <= 0) this._seed += 2147483646;
    }

    // 返回一个在 [0, 1) 范围内的随机浮点数
    nextFloat(): number {
        return (this.next() - 1) / 2147483646;
    }

    // 生成一个新的随机数种子
    private next(): number {
        return this._seed = this._seed * 16807 % 2147483647;
    }
}

export class RandomNormal {
    private _random: Random;
    private _mean: number;
    private _stddev: number;

    constructor(seed: number, mean: number = 0, stddev: number = 1) {
        this._random = new Random(seed);
        this._mean = mean;
        this._stddev = stddev;
    }

    nextFloat(): number {
        let u = 0, v = 0;
        while (u === 0) u = this._random.nextFloat(); //Converting [0,1) to (0,1)
        while (v === 0) v = this._random.nextFloat();
        let num = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
        num = num * this._stddev + this._mean; // Translate to desired mean and standard deviation
        return num;
    }
}
